<?php

namespace App\Http\Controllers\Admin;

use App\Models\CategoryUser;
use App\Models\Goods;
use App\Models\GoodsLog;
use App\Models\GoodsSetmeal;
use App\Models\GoodsSpec;
use App\Services\CommonService;
use App\Services\GoodsService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class GoodsCheckController extends BaseController
{
    /**
     * 商品审核列表
     */
    public function getGoodsCheckList(Request $request){
        $data = $request->input();
        $list = Goods::goodsCheckList($data);
        if($list){
            foreach ($list['data'] as $k=>$v){
                $list['data'][$k]['ischeck_name'] = GoodsService::getGoodsCheckStatus($v['ischeck']);
                $list['data'][$k]['update_time'] = date('Y-m-d H:i:s',$v['update_time']);
                if(!empty($v['activity_type'])){
                    $activity_list = explode(',',$v['activity_type']);
                    foreach ($activity_list as $k1=>$v1){
                        $list['data'][$k]['activity'][$k1]['activity_title'] = GoodsService::getGoodsActivity($v1);
                        $list['data'][$k]['activity'][$k1]['activity_img'] = GoodsService::getGoodsActivityImg($v1);
                    }
                }
                if($v['ischeck'] != GoodsService::GOODS_CHECK_STATUS_REFUSE){
                    $list['data'][$k]['check_reason'] = '';
                }
            }
        }
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$list);
    }

    /**
     * 审核通过
     */
    public function goodsCheckPass(Request $request){
        $goods_id = $request->input('goods_id');
        if($goods_id){
            $goodsid = explode(',',$goods_id);
            foreach ($goodsid as $k=>$v){
                $goods = Goods::where([['id',$v],['is_delete',GoodsService::GOODS_NO_DELETE_STATUS],['ischeck',GoodsService::GOODS_CHECK_STATUS_NO]])->select('id','status','ischeck','checked_status')->first();
                $goods = $goods ? $goods->toArray() : [];
                if(!empty($goods)){
                    if($goods['checked_status'] == GoodsService::GOODS_STATUS_PUT){
                        $data['min_price'] = GoodsSpec::GetMinPrice($goods_id);
                        $data['min_lease_term'] = GoodsSetmeal::getGoodsMinLeaseTerm($goods_id);
                    }
                    $data['status'] = $goods['checked_status'];
                    $data['ischeck'] = GoodsService::GOODS_CHECK_STATUS_CHECKED;
                    $data['update_time'] = time();
                    Goods::where([['id',$v],['is_delete',GoodsService::GOODS_NO_DELETE_STATUS],['ischeck',GoodsService::GOODS_CHECK_STATUS_NO]])->update($data);
                    //添加缓存
                    $goods = Goods::getGoodsInfo($goods_id);
                    if(!empty($goods)){
                        $goods['month_rent'] = ceil($goods['min_price'] * 30);
                        $goods['max_foregift'] = GoodsSetmeal::getMaxForegift($goods_id);
                        $shop_category = CategoryUser::GetShopCategory($goods['member_id']);
                        $goods['category'] = implode('、',array_column($shop_category,'name'));
                        $goods['convey_type'] = '寄出'.GoodsService::GOODS_SEND_CONVEY_TYPE_NAME_LIST[$goods['convey_type1']].'/归还'.GoodsService::GOODS_SEND_CONVEY_TYPE_NAME_LIST[$goods['convey_type2']];
                        unset($goods['convey_type1']);
                        unset($goods['convey_type2']);
                        Redis::hset(GoodsService::GOODS_INFO_KEY,$goods_id,json_encode($goods));
                    }
                    GoodsLog::AddGoodsLog($this->users['id'],$v,'','商品审核通过','');
                }
            }
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'审核成功');
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请选择要操作的商品');
        }
    }

    /**
     * 审核拒绝
     */
    public function goodsCheckRefuse(Request $request){
        $goods_id = $request->input('goods_id');
        $check_reason = $request->input('check_reason');
        if(!empty($goods_id) && !empty($check_reason)){
            $goodsid = explode(',',$goods_id);
            foreach ($goodsid as $k=>$v){
                $goods = Goods::where([['id',$v],['is_delete',GoodsService::GOODS_NO_DELETE_STATUS],['ischeck',GoodsService::GOODS_CHECK_STATUS_NO]])->select('id','status','ischeck','checked_status')->first();
                $goods = $goods ? $goods->toArray() : [];
                if(!empty($goods)){
                    Goods::where([['id',$v],['is_delete',GoodsService::GOODS_NO_DELETE_STATUS],['ischeck',GoodsService::GOODS_CHECK_STATUS_NO]])->update(['ischeck'=>GoodsService::GOODS_CHECK_STATUS_REFUSE,'check_reason'=>$check_reason,'update_time'=>time()]);
                    GoodsLog::AddGoodsLog($this->users['id'],$v,'','商品审核拒绝,原因：'.$check_reason,$check_reason);
                }
            }
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'审核成功');
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请选择要操作的商品并填写拒绝原因');
        }
    }


    /**
     * 商品下架审核列表
     */
    public function getGoodsShelfCheckList(Request $request){
        $data = $request->input();
        $list = Goods::goodsShelfCheckList($data);
        if($list){
            foreach ($list['data'] as $k=>$v){
                $list['data'][$k]['status_name'] = GoodsService::GOODS_STATUS_CHECK_LIST[$v['status']];
                $list['data'][$k]['update_time'] = date('Y-m-d H:i:s',$v['update_time']);
                if(!empty($v['activity_type'])){
                    $activity_list = explode(',',$v['activity_type']);
                    foreach ($activity_list as $k1=>$v1){
                        $list['data'][$k]['activity'][$k1]['activity_title'] = GoodsService::getGoodsActivity($v1);
                        $list['data'][$k]['activity'][$k1]['activity_img'] = GoodsService::getGoodsActivityImg($v1);
                    }
                }
                if($v['status'] != GoodsService::GOODS_STATUS_OFF_REFUSE){
                    $list['data'][$k]['check_reason'] = '';
                }
            }

        }
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$list);
    }

    /**
     * 审核通过
     */
    public function goodsShelfCheckPass(Request $request){
        $goods_id = $request->input('goods_id');
        if($goods_id){
            $goodsid = explode(',',$goods_id);
            foreach ($goodsid as $k=>$v){
                $goods = Goods::where([['id',$v],['is_delete',GoodsService::GOODS_NO_DELETE_STATUS],['status',GoodsService::GOODS_STATUS_APPLY_OFF]])->select('id','status','ischeck','checked_status')->first();
                $goods = $goods ? $goods->toArray() : [];
                if(!empty($goods)){
                    Goods::where([['id',$v],['is_delete',GoodsService::GOODS_NO_DELETE_STATUS],['status',GoodsService::GOODS_STATUS_APPLY_OFF]])->update(['status'=>GoodsService::GOODS_STATUS_OFF,'activity_type'=>'','update_time'=>time()]);
                    GoodsLog::AddGoodsLog($this->users['id'],$v,'','商品下架审核通过','');
                }
            }
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'审核成功');
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请选择要操作的商品');
        }
    }

    /**
     * 审核拒绝
     */
    public function goodsShelfCheckRefuse(Request $request){
        $goods_id = $request->input('goods_id');
        $check_reason = $request->input('check_reason');
        if(!empty($goods_id) && !empty($check_reason)){
            $goodsid = explode(',',$goods_id);
            foreach ($goodsid as $k=>$v){
                $goods = Goods::where([['id',$v],['is_delete',GoodsService::GOODS_NO_DELETE_STATUS],['status',GoodsService::GOODS_STATUS_APPLY_OFF]])->select('id','status','ischeck','checked_status')->first();
                $goods = $goods ? $goods->toArray() : [];
                if(!empty($goods)){
                    Goods::where([['id',$v],['is_delete',GoodsService::GOODS_NO_DELETE_STATUS],['status',GoodsService::GOODS_STATUS_APPLY_OFF]])->update(['status'=>GoodsService::GOODS_STATUS_OFF_REFUSE,'check_reason'=>$check_reason,'update_time'=>time()]);
                    GoodsLog::AddGoodsLog($this->users['id'],$v,'','商品下架审核拒绝,原因：'.$check_reason,$check_reason);
                }
            }
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'审核成功');
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请选择要操作的商品并填写拒绝原因');
        }
    }



}
